﻿//  Created by Mathew Purchase.
//  Copyright (c) 2014 Mathew Purchase. All rights reserved.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class UI_RandomEvent : MonoBehaviour 
{

	//---------------------------------------------------------------------------------------------
	// Properties
	
	
	
	//---------------------------------------------------------------------------------------------
	// Members
	public List<GameObject> m_buttonList;
	public GameObject m_leaveButton;
	public Button m_choiceButton;
	public GameObject m_eventItem;
	public EventHolder m_eventHolder;
	public RandomEventService m_eventMain;
	public GameObject m_eventPanel;
	public Text m_eventDescription;
	public Text m_eventTitle;
	
	
	
	//---------------------------------------------------------------------------------------------
	// Events and Delegates
	
	
	//---------------------------------------------------------------------------------------------
	// Init
	
	void OnEnable () 
	{
		RandomEventService.doEvent += EventFired;
		
	}
	//---------------------------------------------------------------------------------------------
	void OnDisable()
	{
		RandomEventService.doEvent -= EventFired;
		
	}
	
	//---------------------------------------------------------------------------------------------
	// Core Functions

	public void Cleanup()
	{
		for (int a = 0; a < m_buttonList.Count; a++)
		{
			Destroy (m_buttonList[a]);
		}
	}
	//---------------------------------------------------------------------------------------------
	void EventFired()
	{
	
		if (m_eventHolder.canLeave == true)
		{
			m_leaveButton.SetActive(true);
		
		}
		else
		{
			m_leaveButton.SetActive(false);
			
		}
	
		m_eventHolder = m_eventMain.m_eventList[Random.Range(0,m_eventMain.m_eventList.Count)];
	
		
	// change the maintext
		m_eventTitle.text = m_eventHolder.eventTitle;
		m_eventDescription.text = m_eventHolder.descriptionText;
	
	// add the buttons 
	
	 // NASTY
		for (int a = 0; a < m_eventHolder.eventChoiceQuantity; a++)
		{
			Button answer = Instantiate (m_choiceButton, Vector3.zero, Quaternion.identity) as Button;
			answer.GetComponent<RectTransform>().SetParent(m_eventPanel.transform, false);
			m_buttonList.Add(answer.gameObject);
			
			Text buttonNumber = answer.transform.GetChild(0).gameObject.GetComponent<Text>();
			buttonNumber.text = (a+1).ToString();
			Text buttonTitle = answer.transform.GetChild(1).gameObject.GetComponent<Text>();
			buttonTitle.text = m_eventHolder.choiceText[a];
		}
		
		
	}
}
